package main

import (
	"fmt"
	"log"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	_ "github.com/lib/pq"
)

type s struct {
	sno   int    `gorm:"not null"`
	sname string `gorm:"not null"`
}

func main() {
	db, err := gorm.Open("postgres", "host=127.0.0.1 user=postgres dbname=test password=Tss1433223 sslmode=disable")
	if err != nil {
		panic(err)
	}
	fmt.Println("连接成功！")
	defer db.Close()

	if !db.HasTable(&s{}) {
		if err := db.CreateTable(&s{}).Error; err != nil {
			panic(err)
		} else {
			log.Println("创建成功！")
		}
	}

	u1 := s{1, "Li"}
	db.Create(&u1)
	if err := db.Create(&u1).Error; err != nil {
		fmt.Println("插入失败!", err)
		return
	}
}
